package cn.itcast.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Page;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Role;
import cn.itcast.service.Sys_roleService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Slf4j
public class Sys_roleController {

    @Reference(version = "1.0.0")
    private Sys_roleService sys_roleService;

    @GetMapping("/role/page")
    public Result queryRoleList(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = this.sys_roleService.queryRoleList(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
    }
    @PostMapping("/role")
    public Result saveRoles(@RequestBody Map<String,String> map){
        Boolean aBoolean = this.sys_roleService.saveRoles(map);
        if(aBoolean){
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_ROLE_FAIL);
    }

    @GetMapping("/role/{id}")
    public Result queryEditRoles(@PathVariable("id") Integer id){
        Role role = this.sys_roleService.queryEditRoles(id);
        if(ObjectUtil.isNotEmpty(role)){
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }
        return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
    }


    @PutMapping("/role")
    public Result editRoles (@RequestBody Role role){
        Boolean aBoolean = this.sys_roleService.editRoles(role);
        if(aBoolean){
            return new Result(true,MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS,role);
        }
        return new Result(false,MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
    }

    @DeleteMapping("/role/{id}")
    public Result delRoles(@PathVariable("id") Integer id){
        try {
            Boolean aBoolean = this.sys_roleService.delRoles(id);
            if(aBoolean){
                return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
            }
        } catch (Exception e) {
            log.error("删除失败 id = "+ id,e);
        }
        return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
    }

    @GetMapping("/role/all")
    public Result queryRolesAll(){
        try {
            List<Role> roles = this.sys_roleService.queryRolesAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
    }

}
